//
//  ElectronicTicketView.m
//  didacar
//
//  Created by 度周末网络-王腾 on 16/1/13.
//  Copyright © 2016年 dzmmac. All rights reserved.
//

#import "ElectronicTicketView.h"
#import "AppDelegate.h"
#import "SVProgressHUD.h"
#import "UIScreen+Frame.h"
@implementation ElectronicTicketView


+ (ElectronicTicketView *)sharedManager
{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init];
    });
    return _sharedObject;
}


-(id)init{
    
    self = [[[NSBundle mainBundle] loadNibNamed:@"ElectronicTicketView" owner:self options:nil] lastObject];
    self.tap = [[UITapGestureRecognizer alloc ] initWithTarget:self action:@selector(hidden)];
    [self addGestureRecognizer:self.tap];
    
    self.frame = CGRectMake(0, 0, [UIScreen width], [UIScreen height]);
    
    [CommoneTools changeConstraintView:self.qrBgView constraintWidth:GTFixWidthFloat(240)];
    [CommoneTools changeConstraintView:self.qrBgView constraintHeight:GTFixWidthFloat(240)];
    
    
    [CommoneTools changeConstraintView:self.qrImageView constraintWidth:GTFixWidthFloat(200)];
    [CommoneTools changeConstraintView:self.qrImageView constraintHeight:GTFixWidthFloat(200)];
    
    
    self.qrBgView.layer.cornerRadius = 3;
    self.qrBgView.layer.masksToBounds=YES;
    
    self.ciFilterEffect = [[CIFilterEffect alloc ] init];
    return [super init];
    
}

-(void)showTicket:(NSString *)ticketMsg{
    [SVProgressHUD show];
    __weak ElectronicTicketView *weakSelf = self;
    [self.ciFilterEffect initWithQRCodeString:ticketMsg width:GTFixWidthFloat(200) reslutBlock:^(UIImage *QRCodeImage) {
        
        [SVProgressHUD dismiss];
        weakSelf.qrImageView.image =  QRCodeImage;
        [weakSelf show];
    }];
}

-(void)show
{
    
    self.alpha = 0;
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.window addSubview:self];
    
    __weak ElectronicTicketView *weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.alpha = 1;
        
    }];
}

-(void)hidden
{
    __weak ElectronicTicketView *weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

@end
